import {Component, OnInit} from '@angular/core';
import {RunStopsService} from '../../../../../service/run-stops.service';
import {RunStop} from '../../../../../entity/run-stop.model';
import {BladeContainerBase} from '../../../../../shared/blade/blade-container-base';
import {BladeData} from '../../../../../shared/blade/blade-data';
import {Blade} from '../../../../../shared/blade/blade.model';
import {Command} from '../../../../../shared/toolbar.service';
import {DialogResult} from '../../../../../shared/dialog.service';
import {Scheme} from '../../../../../entity/scheme.model';
import {StateInfoEditComponent} from '../state-info-edit/state-info-edit.component'
import {Tag} from '../../../../../entity/tag.model';
import {TagService} from '../../../../../service/tag.service';

@Component({
  selector: 'app-state-info-list',
  templateUrl: './state-info-list.component.html',
  styleUrls: ['./state-info-list.component.css']
})
export class StateInfoListComponent extends BladeContainerBase<Scheme> {

  /**
   * 起停状态集合
   *
   * @type {RunStop[]}
   * @memberof StateInfoListComponent
   */
  runStops: RunStop[] = [];

  /**
   * 当前起停状态
   *
   * @type {RunStop}
   * @memberof StateInfoListComponent
   */
  currentRunStop: RunStop;

  /**
   *测点集合
   *
   * @type {Tag[]}
   * @memberof StateInfoListComponent
   */
  tags: Tag[] = [];

  constructor(
    private runStopsService: RunStopsService,
    private tagService: TagService
  ) {
    super()
  }

  ngOnInit() {
    this.commands = [
      new Command('add', '添加', 'fa fa-plus', 'setofkey:insert', blade => this.exeAdd(blade), blade => this.canExeAdd(blade)),
      new Command('edit', '编辑', 'fa fa-edit', 'setofkey:update', blade => this.exeEdit(blade), blade => this.canExeEdit(blade)),
      new Command('delete', '删除', 'fa fa-trash-o', 'setofkey:delete', blade => this.exeDelete(blade), blade => this.canDelete(blade)),
    ];
    if (this.currentEntity.multipleValue) {
      this.tagService.getTagList()
        .then(res => {
          if (!res) {
            return false
          }
          ;
          this.tags = [...res];
        })
        .catch(err => {
          console.log(err)
        })
    }
    super.ngOnInit();
  }

  initData(): any {
    this.runStopsService.getRunStopsBySchemeId(this.currentEntity.schemeId)
      .then(res => {
        if (res) {
          this.runStops = [...res];
          this.currentRunStop = this.runStops[0];
          this.bladeData.isLoading = false;
        }
      })
      .catch(err => {
        console.log(err)
      })
  }

  exeAdd(blend: Blade): void {
    let data: BladeData = new BladeData();
    data.id = 'modelGroupingCreate';
    data.title = '创建一个状态信息';
    data.isClosingDisabled = false;
    let parentData: ParentData = {
      current: this.currentEntity,
      tags: this.tags,
    }
    let createKeyBlade = this.bladeService.createBlade(StateInfoEditComponent, data, true, {
      runStopId: 0,
      schemeId: this.currentEntity.schemeId
    }, parentData);
    this.bladeService.showBlade(createKeyBlade, this.currentBlade);
  }

  canExeAdd(blend: Blade): boolean {
    return true;
  }

  exeEdit(blend: Blade): void {
    if (this.currentEntity != undefined) {
      let data: BladeData = new BladeData();
      data.id = 'standardCurveEdit';
      data.title = '状态信息';
      let parentData: ParentData = {
        current: this.currentEntity,
        tags: this.tags,
      }
      let editKeyBlade = this.bladeService.createBlade(StateInfoEditComponent, data, false, this.currentRunStop, parentData);
      this.currentBlade.bladeService.showBlade(editKeyBlade, this.currentBlade);
    }
  }

  canExeEdit(blend?: Blade): boolean {
    return this.currentRunStop != null;
  }

  exeDelete(blend: Blade): void {
    let selectedKeys = this.getSelected();
    if (selectedKeys.length > 0) {
      this.dialogService
        .showConfirmationDialog('确认删除', '是否要删除选中项？')
        .subscribe(dialogResult => {
          if (dialogResult == DialogResult.Ok) {
            this.currentBlade.bladeData.isLoading = true;
            this.runStopsService.batchDelete(selectedKeys.map(o => o.runStopId))
              .then(res => {
                if (res.code == '200') {
                  super.refresh(this.currentEntity);
                }
                if (res.code == '8001') {
                  this.dialogService
                    .showConfirmationDialog('删除失败', res.message);
                }
              })
              .catch(error => this.bladeData.error = error)
              .then(res => this.bladeData.isLoading = false);
          }
        })
    }
  }

  canDelete(blend?: Blade): boolean {
    return this.getSelected().length > 0;
  }

  getSelected(): RunStop[] {
    if (this.runStops.length > 0) {
      let filter = this.runStops.filter(ref => ref.isChecked)
      return filter == null ? [] : filter;
    }
    return [];
  }

  onSeletedChange(selectedKey: RunStop) {
    this.currentRunStop = selectedKey;
  }

  onItemCheack(event: HTMLInputElement, key: RunStop) {
    key.isChecked = event.checked;
  }

  onAllCheack(event: HTMLInputElement) {
    if (this.runStops.length > 0) {
      for (let i = 0; i < this.runStops.length; i++) {
        this.runStops[i].isChecked = event.checked;
      }
    }
  }
}

interface ParentData {
  current: Scheme,
  tags: Tag[],
}


// WEBPACK FOOTER //
// ./src/app/module/basic/model/state-info/state-info-list/state-info-list.component.ts
